package com.tianzeculture.stagebao.activity.artist;

import android.content.Context;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.tianzeculture.stagebao.R;
import com.tianzeculture.stagebao.activity.artist.adapter.MyPagerAdapter;
import com.tianzeculture.stagebao.activity.artist.fragment.ArtistDataFragment;
import com.tianzeculture.stagebao.activity.artist.fragment.ArtistPhotoFragment;
import com.tianzeculture.stagebao.activity.artist.fragment.ArtistVideoFragment;
import com.tianzeculture.stagebao.activity.artist.fragment.MyArtistDataFragment;
import com.tianzeculture.stagebao.activity.artist.fragment.MyArtistPhotoFragment;
import com.tianzeculture.stagebao.activity.artist.fragment.MyArtistVideoFragment;
import com.tianzeculture.stagebao.base.BaseActivity;
import com.tianzeculture.stagebao.bean.ArtistsDetailedBean;
import com.tianzeculture.stagebao.bean.MyOfferBean;
import com.tianzeculture.stagebao.net.HomeRestUsage;
import com.tianzeculture.stagebao.net.UserRestUsage;
import com.tianzeculture.stagebao.network.Request;
import com.tianzeculture.stagebao.network.enity.MSG;
import com.tianzeculture.stagebao.network.util.ToastUtil;
import com.tianzeculture.stagebao.util.DensityUtil;
import com.tianzeculture.stagebao.util.Util;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import de.hdodenhof.circleimageview.CircleImageView;

import static com.tianzeculture.stagebao.net.base.AppBaseRestUsageV2.PICTURE_URL;

public class MyArtistActivity extends BaseActivity {

    @BindView(R.id.iv_header_bg_ext)
    ImageView ivHeaderBgExt;
    @BindView(R.id.iv_header_bg)
    ImageView mIvHeaderBg;
    @BindView(R.id.iv_pi_header)
    CircleImageView ivPiHeader;
    @BindView(R.id.rl_header)
    RelativeLayout rlHeader;

    @BindView(R.id.rl_avatar)
    RelativeLayout mRlavatar;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.checkAttention)
    CheckBox checkAttention;
//    @BindView(R.id.fl_toolbar)
//    FrameLayout mFlToolbar;

    @BindView(R.id.title)
    RelativeLayout titleLayout;
    @BindView(R.id.titleTransparent)
    RelativeLayout transparentTitle;

    @BindView(R.id.collapsing_toolbar)
    CollapsingToolbarLayout collapsingToolbar;
    @BindView(R.id.tabLayout)
    TabLayout mTabLayout;
    @BindView(R.id.appBarlayout)
    AppBarLayout mAppBarLayout;
    @BindView(R.id.viewPager)
    ViewPager mViewPager;
    //    @BindView(R.id.tv_pi_name)
//    TextView tvPiName;
    @BindView(R.id.tvArtistName)
    TextView tvArtistName;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_address)
    TextView tvAddress;
    @BindView(R.id.tv_zhiye)
    TextView tvZhiye;
    @BindView(R.id.back)
    ImageButton back;

//    @BindView(R.id.btnSure)
//    Button btnSure;

    @Request
    UserRestUsage userRestUsage = null;
    ArrayList<Fragment> mListFragment;
    private MyPagerAdapter mPagerAdapter;
    String[] titles = {"资料", "照片", "视频"};
    ArtistsDetailedBean bean;
    private int myInfoId = 2;

    private void initData() {
        initToolbar();
        initRefresh();
        mListFragment = new ArrayList<>();
        userRestUsage.myInfo(myInfoId, Util.UserId);
    }


    private void initToolbar() {
        tvTitle.setText("我的主页");
        toolbar.setTitle("");
        setSupportActionBar(toolbar);
        int statusHeight = getStatusHeight(this);

        toolbar.measure(0, 0);
        int measuredHeight = toolbar.getMeasuredHeight();

        ViewGroup.LayoutParams layoutParams = toolbar.getLayoutParams();
        layoutParams.height = measuredHeight + statusHeight;
        toolbar.setLayoutParams(layoutParams);


        ViewGroup.LayoutParams params = titleLayout.getLayoutParams();
        params.height = measuredHeight + statusHeight;
        titleLayout.setLayoutParams(params);

        ViewGroup.LayoutParams params2 = transparentTitle.getLayoutParams();
        params2.height = measuredHeight + statusHeight;
        transparentTitle.setLayoutParams(params2);

    }


    private void initRefresh() {
        mIvHeaderBg.setScaleX(1.3f);
        mIvHeaderBg.setScaleY(1.3f);

        final int[] location = new int[2];
        ivPiHeader.getLocationInWindow(location);
        final int endOffset = DensityUtil.dip2px(this, 200);

        final int[] pointV = new int[2];
        tvArtistName.getLocationInWindow(pointV);
        mAppBarLayout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
            @Override
            public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
                int[] ponitF = new int[2];
                int disten = -verticalOffset;
                int disOffset = endOffset;
                if (disten >= disOffset) {
                    mRlavatar.setAlpha(0);
                    mIvHeaderBg.setScaleX(1.0f);
                    mIvHeaderBg.setScaleY(1.0f);
                    titleLayout.setVisibility(View.VISIBLE);
                    transparentTitle.setVisibility(View.GONE);
                } else if (disten == 0) {
                    mRlavatar.setAlpha(1);
                    mIvHeaderBg.setScaleX(1.3f);
                    mIvHeaderBg.setScaleY(1.3f);
                } else {
                    float precents = (float) (disOffset - disten) / disOffset;
                    mRlavatar.setAlpha(precents);
                    mIvHeaderBg.setScaleX(1.0f + 0.3f * (precents));
                    mIvHeaderBg.setScaleY(1.0f + 0.3f * (precents));
                    titleLayout.setVisibility(View.GONE);
                    transparentTitle.setVisibility(View.VISIBLE);
                }

                ivPiHeader.getLocationInWindow(ponitF);
                int y = ponitF[1];
                if (y > 0) {
                    if (y < endOffset) {
//                        titleLayout.setVisibility(View.GONE);
//                        transparentTitle.setVisibility(View.VISIBLE);
//                        toolbar.setBackgroundResource(R.color.colorTransparent);
                        float precent = (float) (endOffset - y) / endOffset;
                        toolbar.setAlpha(precent);
                    } else if (y >= endOffset) {
                        toolbar.setAlpha(0);
//                        toolbar.setBackgroundResource(R.color.colorGray_F0);
//                        titleLayout.setVisibility(View.GONE);
//                        transparentTitle.setVisibility(View.VISIBLE);
                    }
                } else if (y < 0) {
                    toolbar.setAlpha(1);
//                    toolbar.setBackgroundResource(R.color.colorGray_F0);
//                    titleLayout.setVisibility(View.VISIBLE);
//                    transparentTitle.setVisibility(View.GONE);
                } else if (y == 0) {
//                    titleLayout.setVisibility(View.VISIBLE);
//                    transparentTitle.setVisibility(View.GONE);
//                    toolbar.setBackgroundResource(R.color.colorTransparent);
                    toolbar.setAlpha(1);
                }


            }
        });


    }


    /**
     * 获得状态栏的高度
     *
     * @param context
     * @return
     */
    public int getStatusHeight(Context context) {

        int statusHeight = -1;
        try {
            Class<?> clazz = Class.forName("com.android.internal.R$dimen");
            Object object = clazz.newInstance();
            int height = Integer.parseInt(clazz.getField("status_bar_height")
                    .get(object).toString());
            statusHeight = context.getResources().getDimensionPixelSize(height);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return statusHeight;
    }


    @Override
    public void initUI() {
        super.initUI();
        initData();
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

    @Override
    public void refreshUI(int taskId, MSG msg) {
        super.refreshUI(taskId, msg);
        if (taskId == myInfoId) {
            if (msg.getObj() == null)
                return;
            bean = (ArtistsDetailedBean) msg.getObj();
            Glide.with(this).load(PICTURE_URL + bean.getUser().getImgurl())
                    .error(R.mipmap.icon_head).into(ivPiHeader);
            tvArtistName.setText(bean.getUser().getNickname());
            tvAddress.setText(((bean.getUser().getAddress() == null || bean.getUser().getAddress().equals("")) ? "全国" : bean.getUser().getAddress()) + "|" + ("1".equals(bean.getUser().getSex()) ? "男" : "女"));
            List<MyOfferBean> list = bean.getPriceList();
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < list.size(); i++) {
                if (i == list.size() - 1) {
                    sb.append(list.get(i).getName());
                    continue;
                }
                sb.append(list.get(i).getName());
                sb.append("|");
            }
            tvZhiye.setText(sb.toString());
            mListFragment.add(new MyArtistDataFragment());
            mListFragment.add(new MyArtistPhotoFragment());
            mListFragment.add(new MyArtistVideoFragment());
            mPagerAdapter = new MyPagerAdapter(getSupportFragmentManager(), mListFragment);
            mPagerAdapter.setTitles(titles);
            mViewPager.setAdapter(mPagerAdapter);
            Util.setIndicator(mTabLayout, 25, 25);
            mTabLayout.setupWithViewPager(mViewPager);
        }
    }

    @Override
    protected int getLayoutResID() {
        return R.layout.activity_artist_my;
    }

    public ArtistsDetailedBean getData() {
        return bean == null ? new ArtistsDetailedBean() : bean;
    }
}
